package com.sky.happygos.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.TResult;
import com.sky.happygos.R;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateShopE;
import com.sky.happygos.model.Shop;
import com.sky.happygos.present.ShopP;
import com.sky.happygos.present.UserP;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/22.
 */
@EActivity(R.layout.activity_shop_detail)
public class ShopEditA extends TakePhotoActivity {

    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.sdv_shop_edit_icon)
    SimpleDraweeView icon;

    @ViewById(R.id.et_shop_name)
    EditText name;
    @ViewById(R.id.et_shop_info)
    EditText info;
    @ViewById(R.id.et_shop_school)
    EditText school;
    @ViewById(R.id.et_shop_addr)
    EditText addr;
    @ViewById(R.id.et_shop_notice)
    EditText notice;
    @ViewById(R.id.header_right)
    ImageView commit;


    @Bean
    ShopP shopP;

    Shop shop;

    String path = null;
    boolean isChange = false;

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            isChange = true;
            commit.setVisibility(View.VISIBLE);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    };


    @AfterViews
    public void init(){

        loadView.start();
        shopP.getMyShop();
    }

    @Click(R.id.header_right)
    public void commit(){
        Shop s = new Shop();
        s.id = shop.id;
        s.name = name.getText().toString();
        s.info = info.getText().toString();
        s.school = school.getText().toString();
        s.addr = addr.getText().toString();
        loadView.start();
        shopP.updateShop(path,s);
    }

    @Click(R.id.sdv_shop_edit_icon)
    public void clickPhoto(){
        TakePhoto takePhoto = getTakePhoto();
        CompressConfig compressConfig=new CompressConfig.Builder().setMaxSize(600*800).setMaxPixel(800).create();
        takePhoto.onEnableCompress(compressConfig,true);
        takePhoto.onPickFromGallery();
    }

    public void loadData(){
        icon.setImageURI(ImagePath.to(shop.icon));
        name.setText(shop.name);
        info.setText(shop.info);
        school.setText(shop.school);
        addr.setText(shop.addr);
        if(shop.notice != null && shop.notice.size() > 0) {
            notice.setText(shop.notice.get(shop.notice.size()-1));
        }else{
            notice.setHint("未发布公告");
        }

        name.addTextChangedListener(watcher);
        info.addTextChangedListener(watcher);
        school.addTextChangedListener(watcher);
        addr.addTextChangedListener(watcher);
//        notice.addTextChangedListener(watcher);
    }

    @Subscribe
    public void shopsResult(ShopE e){
        loadView.dismiss();
        if(e.isSuccess){
            List<Shop> shops = e.data;
            if(shops != null && shops.size() > 0){
                shop = shops.get(0);
                loadData();
            }else{
                Toast.makeText(this,"你还未创建商店",Toast.LENGTH_SHORT).show();
            }

        }else{

            Toast.makeText(this,"获取商店信息失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe
    public void updateShopResult(UpdateShopE e){
        loadView.dismiss();
        if(e.isSuccess){
            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }else{
            Toast.makeText(this,"修改失败",Toast.LENGTH_SHORT).show();
        }
    }


    @Click(R.id.header_left)
    public void back(){
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        path = result.getImage().getCompressPath();
        icon.setImageURI(String.format("file://%s",result.getImage().getCompressPath()));
        isChange = true;
        commit.setVisibility(View.VISIBLE);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(this,"获取失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
        Toast.makeText(this,"获取取消",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
